package com.pdg.mcplugin.payd2mine;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import net.milkbowl.vault.economy.Economy;

import com.pdg.mcplugin.common.ArgumentList;
import com.pdg.mcplugin.common.VaultAdapter;
import com.pdg.mcplugin.common.baseclasses.PermissionCheckerBase;
import com.pdg.mcplugin.common.baseclasses.PluginBase;

public class Payd2Mine extends PluginBase {
	
	private static final String COMMAND_P2M = "p2m";

	public Economy getEconomy(){
		return VaultAdapter.getEconomy();
	}
	private Updater updater = null;
	private ConfigurationManager configurationManager = null;
	private BlockListener blockListener = null;
	private DataCache dataCache = null;
	private MaterialTable materialTable = null;
	private MessageManager messageManager = null;
	private CommandProcessor commandProcessor = null;
	private PermissionCheckerBase<Payd2Mine,P2MPermissions> permissionChecker = null;
	
	public PermissionCheckerBase<Payd2Mine,P2MPermissions> getPermissionChecker(){
		if(permissionChecker==null){
			permissionChecker = new PermissionCheckerBase<Payd2Mine,P2MPermissions>(this);
		}
		return permissionChecker;
	}
	
	public Updater getUpdater(){
		if(updater==null){
			updater = new Updater(this);
		}
		return updater;
	}

	@Override
	protected void disablePlugin() {
	}

	@Override
	protected boolean enablePlugin() {
		saveDefaultConfig();
		getServer().getScheduler().scheduleSyncRepeatingTask(this, getUpdater(), getConfigurationManager().getUpdaterDelay(), getConfigurationManager().getUpdaterPeriod());
		getServer().getPluginManager().registerEvents(getBlockListener(), this);
        return true;
	}

	private BlockListener getBlockListener() {
		if(blockListener==null){
			blockListener = new BlockListener(this);
		}
		return blockListener;
	}

	public ConfigurationManager getConfigurationManager() {
		if(configurationManager==null){
			configurationManager = new ConfigurationManager(this);
		}
		return configurationManager;
	}
	
	public DataCache getDataCache(){
		if(dataCache==null){
			dataCache = new DataCache(this);
		}
		return dataCache;
	}

	public MaterialTable getMaterialTable(){
		if(materialTable==null){
			materialTable = new MaterialTable(this);
		}
		return materialTable;
	}
	
	public MessageManager getMessageManager(){
		if(messageManager==null){
			messageManager = new MessageManager(this);
		}
		return messageManager;
	}

	public void reinitialize() {
		configurationManager = null;
		messageManager = null;
		materialTable = null;
		dataCache = null;
	}
	public boolean onCommand(CommandSender theCommandSender, Command theCommand, String theLabel, String[] theArgs){
		ArgumentList theArgumentList = new ArgumentList(theArgs);
		if(theCommand.getName().equalsIgnoreCase(COMMAND_P2M)){
			return getCommandProcessor().processCommand(theCommandSender,theArgumentList);
		}
		return false;
	}

	private CommandProcessor getCommandProcessor() {
		if(commandProcessor==null){
			commandProcessor = new CommandProcessor(this);
		}
		return commandProcessor;
	}

}
